package com.kiwi.cases.login;

import com.kiwi.action.LoginAction;
import com.kiwi.action.SmsCodeAction;
import com.kiwi.constant.Constant;
import com.kiwi.constant.ConstantURL;
import com.kiwi.dao.IndianaDao;
import com.kiwi.util.EncryptSet.Impl.SignatureServiceImpl;
import com.kiwi.util.EncryptSet.Interface.SignatureService;
import com.kiwi.util.RedisUtil;
import com.netease.shockwave.http.HttpHandler;
import com.netease.shockwave.http.HttpUtil;
import com.netease.shockwave.testng.ShockwaveTestNG;
import com.netease.shockwave.validate.AssertUtils;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试接口：注册接口 http://uc.s.kiwi.com/api/mobileReg
 *
 *
 * @author bjwangzheng3
 */
public class MobileRegTest extends ShockwaveTestNG
{
    private  Logger logger = Logger.getLogger(MobileRegTest.class);
    @Autowired
    private LoginAction loginAction;

    @Autowired
    private SmsCodeAction smsCodeAction;

    @BeforeClass
    public void BeforeClassTest()
    {
        logger.info("客户端：注册测试开始");
//        loginAction.iniMobApp();
     }

    @Test(description = "客户端注册接口测试", dataProvider = "MobileRegData")
    public void test(String mobile,String password,String verifyCode,String id,String deviceId) throws Exception {

        String SmsResultCode=smsCodeAction.SmsCodeSend(mobile, Constant.USER_SMS_CODE_TYPE_REGISTER,id,deviceId);
        if(Integer.parseInt(SmsResultCode)==-116) {
                logger.info("删除已注册账号");
                loginAction.delRegInfo(mobile);
                //删除注册信息后，重新获取下验证码
                SmsResultCode=smsCodeAction.SmsCodeSend(mobile, Constant.USER_SMS_CODE_TYPE_REGISTER,id,deviceId);

            }else if(Integer.parseInt(SmsResultCode)<0){//其他异常返回码直接报错
                logger.info("验证码发送失败，请重试：resultcode= "+SmsResultCode);
                //return;
            }
        //验证码设置为空，获取正确验证码，校验正确验证码注册逻辑
        if(verifyCode=="") {
            verifyCode=SmsResultCode;
        }
        String response=loginAction.mobileReg(mobile,password,verifyCode,id,deviceId);
        JSONObject jsonRes = HttpUtil.parseJSONObject(response);
        if(jsonRes.getString("result").equals("100"))//注册成功
        {
           // String sessionId=loginAction.getSessionid(id,deviceId,jsonRes.getString("data"));
            String sessionId=loginAction.getSessionidFromRedis(mobile);
            logger.info("注册成功后登陆的sessionId:"+sessionId);
            //注册成功后未获取到sessionid则认为接口出错
            if(sessionId==null)
                AssertUtils.assertPropertiesEqualsWithMessage("", false,  true);
        }else{//注册失败
            logger.info("注册失败原因："+jsonRes);
        }

    }


    @DataProvider(name = "MobileRegData")
    public static Object[][] data(){
        return new Object[][]{
                //id非法
               // { "18501378014","123456a","","97619702FD3","528C4C26-9C09-49D1-B7D8-4260DB3754DB"},
                //device_id为空
              //  { "18501378014","123456a124","","97619702FD39FC400822F1FAC64F854E01ED1325EC0C8F191EA754D8C9CA018F093454955C51F4B07354AC2060E463B2D437A949D13C6E7FBB7A270C3077E5FCE0485F218D309B1E49CBFF426B1125AC",""},
                //自动获取正确验证码
                { "13501104287","123456a","123456","97619702FD39FC400822F1FAC64F854E01ED1325EC0C8F191EA754D8C9CA018F093454955C51F4B07354AC2060E463B2D437A949D13C6E7FBB7A270C3077E5FCE0485F218D309B1E49CBFF426B1125AC","528C4C26-9C09-49D1-B7D8-4260DB3754DB"},
                //错误验证码
              //  { "18501378015","123456a","123457ass","97619702FD39FC400822F1FAC64F854E01ED1325EC0C8F191EA754D8C9CA018F093454955C51F4B07354AC2060E463B2D437A949D13C6E7FBB7A270C3077E5FCE0485F218D309B1E49CBFF426B1125AC","528C4C26-9C09-49D1-B7D8-4260DB3754DB"},
                };
   }

    @AfterClass
    public void AfterClassTest()
    {

            logger.info("客户端：注册测试结束");
   }


}
